package com.ilincar.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author wujq
 * @Description com.ilincar.utils
 * @Data 03 01 下午4:32
 */
public class SingleHttpClient {
    private static SingleHttpClient ourInstance = new SingleHttpClient();

    public static SingleHttpClient getInstance() {
        return ourInstance;
    }

    private OkHttpClient mClient = null;

    private SingleHttpClient() {
        mClient = new OkHttpClient().newBuilder().build();
    }

    /***
     *
     * @return
     */
    public OkHttpClient getHttpClient() {
        return mClient;
    }

    /***
     * 获取POST请求对象,参数固定为jsondata
     * @param url
     * @param content json串
     * @param clientId mac地址,或clientId
     * @return
     */
    public Request newRequest(String url, String content, String clientId) {
        RequestBody body = new FormBody.Builder()
                .add("jsondata", content)
                .build();
        Request req = new Request.Builder().url(url)
                .post(body)
                .addHeader("appVer", "9")
                .addHeader("clientId", clientId)
                .addHeader("deviceId", clientId)
                .addHeader("os", "android_c")
                .build();
        return req;
    }

    /***
     * 获取POST请求对象,无参数,用于上传文件
     * @param url
     * @param file 文件名称
     * @param clientId mac地址,或clientId
     * @return
     */
    public Request newUploadRequest(String url, File file, String clientId) {
        MultipartBody body = new MultipartBody.Builder()
                .addFormDataPart("jsondata", "{}")
                .addFormDataPart("file", "file",
                        RequestBody.create(MediaType.parse("application/octet-stream"), file))
                .build();
        Request req = new Request.Builder().url(url)
                .post(body)
                .addHeader("appVer", "9")
                .addHeader("clientId", clientId)
                .addHeader("deviceId", clientId)
                .addHeader("os", "android_c")
                .build();
        return req;
    }


    /***
     * 获取POST请求对象,带参数,用于上传一个或多个文件
     * @param url
     * @param params 文件或参数
     * @param clientId mac地址,或clientId
     * @return
     */
    public Request newUploadRequest(String url, final HashMap<String, Object> params, String clientId) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        int c = 0;
        for (String key : params.keySet()) {
            Object object = params.get(key);
            c ++;
            if (object instanceof File) {
                //如果是文件
                File file = (File) object;
                if (file != null && file.exists()) {
                    builder.addFormDataPart(key, key + c, RequestBody.create(MediaType.parse("application/octet-stream"), file));
                }
            } else {
                String p = "";
                try {
                    p = IlincarUtils.setEncodeAndGzip(object.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                builder.addFormDataPart(key, p);
            }
        }

        MultipartBody body = builder.build();
        Request req = new Request.Builder().url(url)
                .post(body)
                .addHeader("appVer", "9")
                .addHeader("clientId", clientId)
                .addHeader("deviceId", clientId)
                .addHeader("os", "android_c")
                .build();
        return req;
    }

    /***
     * 获取GET请求对象
     * @param url
     * @param clientId mac地址,或clientId
     * @return
     */
    public Request newRequest(String url, String clientId) {

        Request req = new Request.Builder().url(url)
                .addHeader("appVer", "9")
                .addHeader("clientId", clientId)
                .addHeader("deviceId", clientId)
                .addHeader("os", "android_c")
                .build();
        return req;
    }
}
